package com.slipper.system.codeless.service;

import java.util.List;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.slipper.util.Tool;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;

import com.slipper.system.codeless.dao.PageCompButtonConditionDao;
import com.slipper.system.codeless.bo.PageCompButtonConditionBO;

import com.slipper.core.transaction.ReadTransactional;
import com.slipper.core.transaction.WriteTransactional;
import com.slipper.util.UUID;

@Service
@ReadTransactional
public class PageCompButtonConditionService {
    @Resource
    private PageCompButtonConditionDao pageCompButtonConditionDao;
    
    public PageCompButtonConditionBO queryBy(String id) {
        return pageCompButtonConditionDao.queryBy(id);
    }
    
    public List<PageCompButtonConditionBO> queryList(PageCompButtonConditionBO bo) {
        return pageCompButtonConditionDao.queryList(bo);
    }
    
    public PageInfo<PageCompButtonConditionBO> queryPage(PageCompButtonConditionBO bo) {
        PageHelper.startPage(bo.getPageNumber(), bo.getPageSize());
        return new PageInfo<>(pageCompButtonConditionDao.queryList(bo));
    }
    
    @WriteTransactional
    public int save(PageCompButtonConditionBO bo) {
        if (Tool.isNotBlank(bo.getId())) {
            bo.setUpdateTime(System.currentTimeMillis());
            return pageCompButtonConditionDao.update(bo);
        } else {
            bo.setId(UUID.getUUID());
            bo.setUpdateTime(System.currentTimeMillis());
            bo.setCreateTime(System.currentTimeMillis());
            return pageCompButtonConditionDao.insert(bo);
        }
    }

    @WriteTransactional
    public int save(String buttonId, List<PageCompButtonConditionBO> list, String employeeId) {
        if (Tool.isBlank(buttonId)) {
            return 0;
        }

        for (int i=0;i<list.size(); i++) {
            PageCompButtonConditionBO condition = list.get(i);
            condition.setButtonId(buttonId);
            condition.setOrdinal(i + 1);
            if (Tool.isBlank(condition.getId())) {
                condition.setId(UUID.getUUID());
                condition.setUpdateId(employeeId);
                condition.setCreateId(employeeId);
                pageCompButtonConditionDao.insert(condition);
            } else {
                condition.setUpdateId(employeeId);
                pageCompButtonConditionDao.update(condition);
            }
        }

        // 删除被删除的数据
        PageCompButtonConditionBO qo = new PageCompButtonConditionBO();
        qo.setButtonId(buttonId);
        pageCompButtonConditionDao.queryList(qo).stream()
                .filter(condition -> list.stream().noneMatch(temp -> condition.getId().equals(temp.getId())))
                .forEach(this::delete);

        return 1;
    }

    @WriteTransactional
    public void saveList(List<PageCompButtonConditionBO> list) {
        if (list == null) return;

        list.forEach(item -> {
            if (pageCompButtonConditionDao.queryBy(item.getId()) != null) {
                item.setUpdateTime(System.currentTimeMillis());
                pageCompButtonConditionDao.update(item);
            } else {
                item.setUpdateTime(System.currentTimeMillis());
                item.setCreateTime(System.currentTimeMillis());
                pageCompButtonConditionDao.insert(item);
            }
        });
    }
    
    @WriteTransactional
    public int delete(PageCompButtonConditionBO bo) {
        return pageCompButtonConditionDao.delete(bo);
    }

    @WriteTransactional
    public void deleteByButtonId(String buttonId) {
        if (Tool.isBlank(buttonId)) {
            return;
        }

        PageCompButtonConditionBO qo = new PageCompButtonConditionBO();
        qo.setButtonId(buttonId);
        pageCompButtonConditionDao.queryList(qo).forEach(this::delete);
    }

    public List<PageCompButtonConditionBO> queryByButtonId(String buttonId) {
        PageCompButtonConditionBO qo = new PageCompButtonConditionBO();
        qo.setButtonId(buttonId);
        return pageCompButtonConditionDao.queryList(qo);
    }

    @WriteTransactional
    public void copyByButtonId(String oldButtonId, String newButtonId, String employeeId) {
        queryByButtonId(oldButtonId).forEach(condition -> {
            condition.setId(UUID.getUUID());
            condition.setButtonId(newButtonId);
            condition.setUpdateId(employeeId);
            condition.setCreateId(employeeId);
            condition.setUpdateTime(System.currentTimeMillis());
            condition.setCreateTime(System.currentTimeMillis());
            pageCompButtonConditionDao.insert(condition);
        });
    }
}
